/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package olympicgamesgui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Collections;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import olympicgameslogic.*;

/**
 *
 * @author Fabio Mendonca
 */
public class DialogSportsMain extends JFrame {

   private JPanel pNorte, pMod, pMbt, pCentro;
   private JScrollPane painel1;
   private JLabel nome;
   private JTable tabela1;
   private JButton addButton, deleteButton, manageButton;
   private static int linha;
   private ClassLoader cldr = this.getClass().getClassLoader();
/// variaveis para a tabela subsituir por dados recebidos em array
   String[] columnNames = {"Modality", "Total disciplines"};
   private LinkedList<Modality> confModalities;
   private DefaultTableModel TableModel;
   private Modality mod_t;

   public DialogSportsMain(String titulo) {
      super(titulo);
      super.setIconImage(Toolkit.getDefaultToolkit().getImage(cldr.getResource("images/extra/icon.png")));
      setLayout(new BorderLayout());
      confModalities = OlympicGamesGui.ap.getModalities();

      pNorte = new JPanel(new FlowLayout(FlowLayout.CENTER));
      pNorte.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));

      nome = new JLabel();
      nome.setText("Modalities");
      nome.setFont(new Font("Arial", Font.PLAIN + Font.BOLD, 20));
      ImageIcon img = new ImageIcon(cldr.getResource("images/extra/page_info.png"));
      nome.setIcon(img);
      pNorte.add(nome);

      pNorte.setBackground(Color.WHITE);
      add(pNorte, BorderLayout.NORTH);
//barra com botões
      pMod = new JPanel(new BorderLayout());
      pMbt = new JPanel(new FlowLayout());
      addButton = new JButton("New", new ImageIcon(cldr.getResource("images/extra/page_add.png")));
      deleteButton = new JButton("Delete", new ImageIcon(cldr.getResource("images/extra/page_delete.png")));
      manageButton = new JButton("Manage", new ImageIcon(cldr.getResource("images/extra/page_edit.png")));
      pMbt.add(addButton);
      pMbt.add(deleteButton);
      pMbt.add(manageButton);

//criar tabela atraves do arraylist           

      //Tabela fazer codigo para carregad dados relativos a um determinado país.   

      tabela1 = new JTable() {

	 public boolean isCellEditable(int rowIndex, int colIndex) {
	    return false; //Disallow the editing of any cell
	 }
      };
      TableModel = (DefaultTableModel) tabela1.getModel();
      TableModel.setColumnIdentifiers(columnNames);
      setUpTableData();
      JTableHeader header = tabela1.getTableHeader();
      header.setBackground(Color.DARK_GRAY);
      header.setFont(new Font("Arial", Font.BOLD, 12));
      header.setForeground(Color.WHITE);
      tabela1.setPreferredScrollableViewportSize(new Dimension(300, 70));
      tabela1.setFillsViewportHeight(true);
      tabela1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      tabela1.getSelectionModel().addListSelectionListener(new DialogSportsMain.RowListener3());
      tabela1.getTableHeader().setReorderingAllowed(false);


//construir painel para tab de atletas  
      pMod.add(pMbt, BorderLayout.NORTH);
      painel1 = new JScrollPane(tabela1);
      pMod.add(painel1, BorderLayout.CENTER);
      pCentro = new JPanel(new BorderLayout());
      pCentro.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
      pCentro.add(pMbt, BorderLayout.NORTH);
      pCentro.add(pMod, BorderLayout.CENTER);
      add(pCentro, BorderLayout.CENTER);


      setMinimumSize(new Dimension(450, 400));
      setSize(450, 400);
      setVisible(true);
      setDefaultCloseOperation(DISPOSE_ON_CLOSE);
      setLocationRelativeTo(null);


      addWindowListener(new WindowAdapter() {

	 @Override
	 public void windowClosing(WindowEvent e) {
	    dispose();
	 }
      });
//        Eventos dos botoões
      //Botão Criar novo        
      addButton.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    String modality = JOptionPane.showInputDialog(DialogSportsMain.this, "Create Modality", "Modality Add", JOptionPane.OK_OPTION);
	    if (modality == null) {
	       return;
	    } else if (OlympicGamesGui.ap.getModality(modality.trim()) != null) {
	       JOptionPane.showMessageDialog(DialogSportsMain.this, "Modality Already Exists", "Error", JOptionPane.WARNING_MESSAGE);
	       return;
	    } else {
	       OlympicGamesGui.ap.addModality(new Modality(modality.trim()));
	       refreshTable();
	    }
	 }
      });

//Botão apagar 
      deleteButton.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    if (mod_t != null) {
	       int i = JOptionPane.showConfirmDialog(DialogSportsMain.this, "Remove Modality\nConfirm?", "Remove Delegation", JOptionPane.YES_NO_OPTION, JOptionPane.YES_NO_OPTION);
	       if (i == JOptionPane.YES_OPTION) {
		  confModalities.remove(mod_t);
		  refreshTable();
		  JOptionPane.showMessageDialog(DialogSportsMain.this, "Modality Removed");
		  mod_t = null;
	       }
	    } else {
	       JOptionPane.showMessageDialog(DialogSportsMain.this, "No Modality Selected");
	    }
	 }
      });

      manageButton.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {

	    if (mod_t == null) {
	       JOptionPane.showMessageDialog(DialogSportsMain.this, "No Modality Selected");
	    } else {
	       DialogManageModalities jan = new DialogManageModalities("Manage Disciplines", mod_t);
	    }
	 }
      });

      addWindowListener(new WindowAdapter() {

	 @Override
	 public void windowClosing(WindowEvent e) {
	    dispose();
	 }
      });
   }

   private class RowListener3 implements ListSelectionListener {

      @Override
      public void valueChanged(ListSelectionEvent event) {
	 if (event.getValueIsAdjusting()) {
	    return;
	 }
	 linha = tabela1.getSelectedRow();
	 if (linha == -1) {
	    linha = 0;
	 } else {
	    for (Modality d : confModalities) {
	       if (tabela1.getValueAt(linha, 0).toString().equals(d.getName().toString())) {
		  mod_t = d;
	       }
	    }
	 }
      }
   }

   private void setUpTableData() {
      Object[][] data1 = new Object[confModalities.getSize()][columnNames.length];
      int i = 0;
      LinkedList<Modality> tempEd = confModalities;

      for (Modality s : tempEd) {
	 data1[i][0] = s.getName();
	 data1[i][1] = s.getDisciplines().size();
	 TableModel.addRow(data1[i]);
	 i++;
      }
      tabela1.setModel(TableModel);

   }

   public void refreshTable() {
      TableModel.setRowCount(0);
      tabela1.removeAll();
      setUpTableData();
   }
}